package app.netmorphgui.tabs;

import java.awt.Color;
import java.awt.Component;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;

public class JMapTable extends JTable {

	List<String> keyList;
	Map<String, String> data;
	List<String> highlightList;
	Map<String, String> highlightTooltip;
	
	MapTableModel model;
	
	private class MapTableModel extends DefaultTableModel {
		@Override
		public int getColumnCount() {
			return 2;
		}
		@Override
		public int getRowCount() {
			return keyList.size();
		}
		@Override
		public Object getValueAt(int row, int column) {
			String key = keyList.get(row);
			if (column == 0) return key;
			String value = data.get(key);
			if (value == null) return ""; else return value;
		}
		@Override
		public void setValueAt(Object aValue, int row, int column) {
			String key = keyList.get(row);
			if (column == 0) return;
			data.put(key, (String) aValue);
		}
		@Override
		public boolean isCellEditable(int row, int column) {
			if (column == 0) return false;
			return true;
		}
		@Override
		public String getColumnName(int column) {
			return new String[] { "Key", "Value" }[column];
		}
	}
	
	public void set(String key, String value) {
		if (!keyList.contains(key)) keyList.add(key);
		data.put(key, value);
	}
	
	public void update()
	{
		model.fireTableDataChanged();
	}
	
	public class Renderer extends JLabel implements TableCellRenderer {

		public Renderer() {
			setOpaque(true);
		}
		
		@Override
		public Component getTableCellRendererComponent(JTable table,
				Object value, boolean isSelected, boolean hasFocus, int row,
				int column) 
		{
			String key = (String) getValueAt(row, 0);
			if (highlightList.contains(key)) {
				setBackground(new Color(255, 255, 128));
				setToolTipText(highlightTooltip.get(key));
			} else {
				setBackground(Color.white);
				setToolTipText(null);
			}
			setText((String)value);
			return this;
		}
		
	}
	
	private void init()
	{
		model = new MapTableModel();
		setModel(model);
		getColumnModel().getColumn(0).setMinWidth(280);
		getColumnModel().getColumn(1).setMinWidth(120);
		getColumnModel().getColumn(0).setCellRenderer(new Renderer());
		getColumnModel().getColumn(1).setCellRenderer(new Renderer());
		
	}

	public JMapTable() {
		data = new HashMap<String, String>();
		keyList = new ArrayList<String>();
		highlightList = new ArrayList<String>();
		highlightTooltip = new HashMap<String, String>();
		
		init();
//		setPreferredSize(new Dimension(300, 400));
	}

	public JMapTable(Map<String, String> data, List<String> keys) {
		this.data = data;
		this.keyList = keys;
		highlightList = new ArrayList<String>();
		highlightTooltip = new HashMap<String, String>();
		
		init();
//		setPreferredSize(new Dimension(300, 400));
	}

	public void setData(Map<String, String> data, List<String> keys) {
		this.data = data;
		this.keyList = keys;
		highlightList = new ArrayList<String>();
		highlightTooltip = new HashMap<String, String>();

		init();
	}
}
